package sg.edu.nus.comp.icreate.ivletalk.events;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.util.Log;

import sg.edu.nus.comp.icreate.ivletalk.utils.xml.AbstractXmlParser;

public class EventsXmlParser extends AbstractXmlParser {

	private static final String TAG_EVENT_TITLE = "Title";
	private static final String TAG_EVENT_DESCRIPTION = "Description";
	private static final String TAG_EVENT_CONTACT = "Contact";
	private static final String TAG_EVENT_PRICE = "Price";
	private static final String TAG_EVENT_ORGANIZER = "Organizer";
	private static final String TAG_EVENT_VENUE = "Venue";
	private static final String TAG_EVENT_ID = "ID";
	private static final String TAG_EVENT_DATE = "EventDateTime";
	private static final String TAG_EVENT = "Data_StudentEvents";

	public EventsXmlParser(InputStream input) throws XmlPullParserException {
		super(input);
	}

	public EventsXmlParser() {
		super();
	}

	public Object getResult() {
		int eventType;
		List<Event> eventList = new ArrayList<Event>();
		try {
			goToTag(TAG_RESULT, XmlPullParser.START_TAG);
			Event event = new Event();

			do {
				eventType = parser.getEventType();
				if (eventType == XmlPullParser.START_TAG && TAG_EVENT.equals(parser.getName())) {
					event = new Event();
				} else if (eventType == XmlPullParser.START_TAG && TAG_EVENT_TITLE.equals(parser.getName())) {
					parser.next();
					event.setTitle(parser.getText());
				} else if (eventType == XmlPullParser.START_TAG && TAG_EVENT_ID.equals(parser.getName())) {
					parser.next();
					event.setID(parser.getText());
				} else if (eventType == XmlPullParser.START_TAG && TAG_EVENT_DESCRIPTION.equals(parser.getName())) {
					parser.next();
					event.setDescription(parser.getText());
				} else if (eventType == XmlPullParser.START_TAG && TAG_EVENT_VENUE.equals(parser.getName())) {
					parser.next();
					event.setVenue(parser.getText());
				} else if (eventType == XmlPullParser.START_TAG && TAG_EVENT_DATE.equals(parser.getName())) {
					parser.next();
					event.setDate(parser.getText());
				} else if (eventType == XmlPullParser.START_TAG && TAG_EVENT_ORGANIZER.equals(parser.getName())) {
					parser.next();
					event.setOrganizer(parser.getText());
				} else if (eventType == XmlPullParser.START_TAG && TAG_EVENT_PRICE.equals(parser.getName())) {
					parser.next();
					event.setPrice(parser.getText());
				} else if (eventType == XmlPullParser.START_TAG && TAG_EVENT_CONTACT.equals(parser.getName())) {
					parser.next();
					event.setContact(parser.getText());
				} else if (eventType == XmlPullParser.END_TAG && TAG_EVENT.equals(parser.getName())) {
					eventList.add(event);

				}
				parser.next();
			} while (parser.getEventType() != XmlPullParser.END_TAG || !TAG_RESULT.equals(parser.getName()));

		} catch (Exception e) {
			Log.e("error", "Error in parse xml", e);
		}

		return eventList;
	}

}
